layui.use('flow', function(){
    var $ = layui.jquery; //不用额外加载jQuery，flow模块本身是有依赖jQuery的，直接用即可。
    var flow = layui.flow;

    // 点击搜索框查询
    $("#searchBtn").click(function (){
        var tag=$("#search").val();
        // 清除分类选项样式
        $(".item").css({"background-color":"#f7eff1","color":"#C95B45"});
        flowfunction({search:$("#search").val(),tag:""});
        if(tag!=null && tag.length!=0){
            $.ajax({
                url:"/mywechat/searchtags",
                data:{tagname:tag,type: 8},
                success:function (data){
                    console.log(data);
                }
            })
        }

    })
    // // 点击分类查询
    // $(".item").click(function(){
    //     $(this).css({"background-color":"#4D454D","color":"#efefef"}).siblings().css({"background-color":"#F7D6AA","color":"black"});
    //     var categoryname=$(this).find("#categoryname")[0].innerHTML;
    //     flowfunction({productName: $("#search").val(),categoryName:categoryname});
    // })

    // 点击分类查询
    $(".item").click(function(){
        $(this).css({"background-color":"#4D454D","color":"#efefef"}).siblings().css({"background-color":"#F7EFF1","color":"#C95B45"});
        var tag=$(this).find("#categoryname")[0].innerHTML;
        flowfunction({search: $("#search").val(),tag:tag});
        $.ajax({
            url:"/mywechat/searchtags",
            data:{tagname:tag,type:8},
            success:function (data){
                console.log(data);
            }
        });
    })


    window.flowfunction=function (obj){
        $("#list").html("");
        flow.load({
            elem: '#list' //指定列表容器
            ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
                var list = [];


                //以jQuery的Ajax请求为例，请求下一页数据（注意：page是从2开始返回）
                $.post( '/factory/getfactory',{page:page,limit:10,search:obj.search,tag:obj.tag}, function(res){
                    console.log(res);
                    //假设你的列表返回在data集合中
                    layui.each(res.data.records, function(index, item){
                        console.log(item);
                        let pic=[];
                        pic=item.images.split("|_|");
                        let div="<a href='/factory/factoryarticledetail?id="+item.id + "' class='gohref'>"
                        div=div+"<div class='factorybox'>";
                        div=div+"<div class='factoryimg'>";
                        div=div+ "<img src="+pic[0]+">";
                        div=div+ "</div>";
                        div+="<div class='factorycontent'>";
                        div+="<div class='factorytitle'>";
                        div +=item.title
                        div=div+ "</div>";
                        div+="<div class='information'>";
                        if(item.tags.length>0 && item.tags!=null) {
                            div+="<div class='tags'>";
                            div+=item.tags
                            div=div+ "</div>";
                        }

                        div+="<div class='stars s5'></div>"
                        div=div+ "</div>";
                        div=div+ "</div>";
                        div+="</div>";
                        div=div+ "</a>";

                        list.push(div);
                    });
                    //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                    //totalPage为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                    next(list.join(''), page < res.data.size);
                },'json');
            }
        });
    }

    flowfunction({search: "",tag: ""});

});
